package com.daisuxia.web.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.daisuxia.back.utils.ServiceResult;
import com.daisuxia.constant.Constant;
import com.daisuxia.web.common.Certification.IHttpCertification;
import com.daisuxia.web.common.reslult.JsonResult;
import com.daisuxia.web.common.reslult.Status;
import com.daisuxia.web.dao.IPaginationDao;
import com.daisuxia.web.dao.IUserBankDao;
import com.daisuxia.web.dao.IUserDao;
import com.daisuxia.web.dao.UserCerticationDao;
import com.daisuxia.web.pojo.User;
import com.daisuxia.web.pojo.UserCardInfo;
import com.daisuxia.web.util.PageConfig;

import redis.clients.jedis.JedisCluster;

@Service
public class UserBankService implements IUserBankService {
	Logger logger = Logger.getLogger(UserBankService.class);
	@Autowired
	private IPaginationDao paginationDao;
	@Autowired
	@Qualifier("userBankDaoImpl")
	private IUserBankDao userBankDao;
	@Autowired
	@Qualifier("userCertication")
	private UserCerticationDao userCerticationDao;
	@Autowired
	private IHttpCertification httpCertification;
	@Autowired
	private IUserDao userDao;
	@Autowired
	JedisCluster jedisCluster;

	@Autowired
	private IJsStepRecordService jsStepRecordService;

	@Override
	public Map<String, String> selectBankByPrimaryKey(Integer id){
		return userBankDao.selectByPrimaryKey(id);
	}

	@Override
	public JsonResult saveUserBankCard(Map<String, String> params) {
		JsonResult result = checkBankParmas(params);
		if (result.isSuccessed()) {
			result = httpCertification.bankCard(params);
			if (result.isSuccessed()) {
				Map<String, String> bankInfo = userBankDao.selectByPrimaryKey(Integer.parseInt(params.get("bank_id")));
				HashMap<String, Object> bankMap = new HashMap<String, Object>();
				bankMap.put("userId", Integer.parseInt(params.get("userId")));
				List<UserCardInfo> cardListr = userBankDao.findUserCardByUserId(bankMap);
				UserCardInfo cardInfo = new UserCardInfo();
				cardInfo.setBank_id(Integer.parseInt(params.get("bank_id")));
				cardInfo.setCard_no(params.get("card_no"));
				cardInfo.setPhone(params.get("phone"));
				cardInfo.setUserId(Integer.parseInt(params.get("userId")));
				cardInfo.setOpenName(params.get("realName"));
				cardInfo.setBankName(bankInfo.get("bankName") == null ? "" : bankInfo.get("bankName"));
				cardInfo.setStatus(UserCardInfo.STATUS_DEFAULT);
				cardInfo.setMainCard(UserCardInfo.MAINCARD_Z);
				cardInfo.setType(UserCardInfo.TYPE_DEBIT);
				cardInfo.setCreateTime(new Date());
				boolean falge = false;
				if (cardListr != null && cardListr.size() > 0) {
					falge = userBankDao.updateUserBankCard(cardInfo);
				} else {
					falge = userBankDao.saveUserbankCard(cardInfo);
				}
				if (!falge) {
					result.setCode("-1");
					result.setMsg("保存失败");
				} else {
//					try {
//						User user = userDao.searchByUserid(Integer.parseInt(params.get("userId")));
//						String invitePhone = null;
//						if (user.getInviteUserid() != null) {
//							Map<String, Object> map = new HashMap<String, Object>();
//							map.put("id", Integer.parseInt(user.getInviteUserid()));
//							User userInvit = userDao.searchByUphoneAndUid(map);
//							invitePhone = userInvit.getUserPhone();
//						}
//						HashMap<String, String> stepMap = new HashMap<String, String>();
//						stepMap.put("userName", user.getRealname());
//						stepMap.put("cardNum", user.getIdNumber());
//						stepMap.put("userPhone", user.getUserPhone());
//						stepMap.put("awardType", "APPROVE");
//						stepMap.put("invitePhone", invitePhone);
//
//						logger.info(" addUserStep   APPROVE 请求参数：" + stepMap.toString());
//						ServiceResult serviceResult = jsStepRecordService.addUserStep(stepMap);
//						logger.info("addUserStep 抽奖返回>>>：" + serviceResult.getCode() + ":" + serviceResult.getMsg());
//
//					} catch (Exception e) {
//						logger.info("addUserStep error APPROVE", e);
//					}
				}
			}
		}
		return result;
	}

	@Override
	public JsonResult updateUpserBankCard(Map<String, String> params) {
		JsonResult result = checkBankParmas(params);
		if (result.isSuccessed()) {
			result = httpCertification.bankCard(params);
			if (result.isSuccessed()) {
				Map<String, String> bankInfo = userBankDao.selectByPrimaryKey(Integer.parseInt(params.get("bank_id")));
				UserCardInfo cardInfo = new UserCardInfo();
				cardInfo.setId(params.get("id")==null?0:Integer.parseInt(params.get("id")));
				cardInfo.setBank_id(Integer.parseInt(params.get("bank_id")));
				cardInfo.setCard_no(params.get("card_no"));
				cardInfo.setPhone(params.get("phone"));
				cardInfo.setUserId(Integer.parseInt(params.get("userId")));
				cardInfo.setOpenName(params.get("realName"));
				cardInfo.setBankName(bankInfo.get("bankName") == null ? "" : bankInfo.get("bankName"));
				cardInfo.setStatus(UserCardInfo.STATUS_DEFAULT);
				cardInfo.setMainCard(UserCardInfo.MAINCARD_Z);
				cardInfo.setType(UserCardInfo.TYPE_DEBIT);
				cardInfo.setUpdateTime(new Date());
				result.setData(cardInfo);
				// boolean flage=userBankDao.updateUserBankCard(cardInfo);
				// if(flage){
				// }else{
				// result.setMsg("重新绑定卡失败");
				// result.setCode("-1");
				// }
			}
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> findAllBankInfo() {
		return this.userBankDao.findAllBankInfo();
	}

	/**
	 * 验证绑定银行卡参数
	 * 
	 * @param params
	 * @return
	 */
	public static JsonResult checkBankParmas(Map<String, String> params) {
		JsonResult result = new JsonResult(Status.FAILD.getName(), Status.FAILD.getValue());
		if (params.get("bank_id") == null || "".equals(params.get("bank_id").toString())) {
			result.setMsg("银行编号不能为空");
		} else if (params.get("card_no") == null || "".equals(params.get("card_no").toString())) {
			result.setMsg("银行卡不能为空");
		} else if (params.get("phone") == null || "".equals(params.get("phone").toString())) {
			result.setMsg("预留手机号码不能为空");
		} else {
			result = new JsonResult(Status.SUCCESS.getName(), Status.SUCCESS.getValue());
		}
		return result;
	}

	@Override
	public List<UserCardInfo> findUserCardByUserId(HashMap<String, Object> params) {
		return userBankDao.findUserCardByUserId(params);
	}

	@Override
	public PageConfig<Map<String, String>> findAllUserBankCardList(HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "UserCardInfo");
		return paginationDao.findPage("findUserBankList", "findUserBankTotal", params, "back");
	}

	@Override
	public boolean updateUserBankCard(UserCardInfo cardInfo) {
		return userBankDao.updateUserBankCard(cardInfo);
	}
}
